
//
//  YHYSaySomthingViewController.m
//  LANGirl
//
//  Created by 杨泓晔 on 16/3/21.
//
//

#import "YHYSaySomthingViewController.h"

@interface YHYSaySomthingViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UIButton *goBack;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, retain) UITextView *textV;
@property (nonatomic, retain) UITextField *nameTextField;
@property (nonatomic, retain) UILabel *numLabel;
@property (nonatomic, assign) BOOL isSelect;
@property (nonatomic, retain) UILabel *uilabel;
@property (nonatomic, assign) NSInteger intA;
@property (nonatomic, assign) NSInteger numChiese;
@end

@implementation YHYSaySomthingViewController

- (void)viewWillAppear:(BOOL)animated {
    
    self.title = @"我的说说";
    self.tabBarController.tabBar.hidden=YES;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isSelect = YES;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = LV;
    //左侧的图标
    self.goBack = [UIButton buttonWithType:UIButtonTypeCustom];
    self.goBack.frame = CGRectMake(0, 0, 38 * AUTO_WIDTH, 38 * AUTO_HEIGHT);
    [self.goBack setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.goBack addTarget:self action:@selector(Back:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_goBack];
    
    
    self.saveButton = [UIButton new];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_saveButton];
    [self.saveButton setImage:[UIImage imageNamed:@"queren"] forState:UIControlStateNormal];

    self.saveButton.frame = CGRectMake(self.view.frame.size.width - 40 * AUTO_WIDTH, 0, 30 * AUTO_WIDTH, 30 * AUTO_HEIGHT);
    [self.saveButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //bigView
    self.textV = [UITextView new];
    [self.view addSubview:self.textV];
    self.textV.backgroundColor = [UIColor whiteColor];
    [self.textV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(25 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(150 * AUTO_HEIGHT);
        
    }];
    self.textV.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textV.delegate = self;
    self.textV.font = [UIFont systemFontOfSize:TEXTCONTENTFONT*TEXTWEIGHT];
    self.textV.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.textV.userInteractionEnabled = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.uilabel = [UILabel new];
    
    [self.textV addSubview:self.uilabel];
    self.uilabel.text = @"一句话介绍自己，和大家打声招呼，写写现在的心情";
    
    self.uilabel.enabled = NO;//lable必须设置为不可用
    self.uilabel.font = [UIFont systemFontOfSize:TEXTLABELFONT*TEXTWEIGHT];
    self.uilabel.frame = CGRectMake(10, 10 * AUTO_HEIGHT, self.view.frame.size.width - 20 * AUTO_WIDTH, 12 * AUTO_HEIGHT);
//    
    self.numLabel = [UILabel new];
    [self.view addSubview:self.numLabel];
    self.numLabel.textColor = [UIColor blackColor];
    self.numLabel.alpha = 0.6;
    self.numLabel.font = [UIFont systemFontOfSize:TEXTLABELFONT*TEXTWEIGHT];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textV.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view.mas_right).offset( - 50  * AUTO_WIDTH);
        make.height.mas_equalTo(10 * AUTO_WIDTH);
        
    }];
    
    self.numLabel.text = @"0/40";
    
//
    
   
    
    

}


//判断是否为汉字
- (BOOL)isChinesecharacter:(NSString *)string{
    if (string.length == 0) {
        return NO;
    }
    unichar c = [string characterAtIndex:0];
    if (c >=0x4E00 && c <=0x9FA5)
    {         return YES;
        //汉字
    }     else
    {         return NO;
        //英文
    }
}
    //计算汉字的个数
- (NSInteger)chineseCountOfString:(NSString *)string{
    int ChineseCount = 0;
    if (string.length == 0) {
        return 0;
    }
    for (int i = 0; i<string.length; i++) {
        unichar c = [string characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FA5)
        {
            ChineseCount++ ;
            //汉字
        }
    }    return ChineseCount;
}
//计算字母的个数
- (NSInteger)characterCountOfString:(NSString *)string{
    int characterCount = 0;
    if (string.length == 0)
    {        return 0;
    }
    for (int i = 0; i<string.length; i++) {
        unichar c = [string characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FA5)
        {
            
        }
        else
            
{            characterCount++;
    //英文
}
}
    return characterCount;
}



-(void)textViewDidChange:(UITextView *)textView
{
    self.textV.text = textView.text;
    if (textView.text.length == 0) {
        self.uilabel.text = @"一句话介绍自己，和大家打声招呼，写写现在的心情";
    }else{
        self.uilabel.text = @"";
    }
    
    self.intA =[self characterCountOfString:self.textV.text];
    self.numChiese = [self chineseCountOfString:self.textV.text];
    self.numLabel.text = [NSString stringWithFormat:@"%ld/40",(self.numChiese + self.intA)];
    
    

}








- (void)Back:(UIButton *)btu{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveButton:(UIButton *)btu{
    
    if ((self.numChiese + self.intA) >= 40) {
//        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"文字过多" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
    else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@";;;;");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
